using System;
using System.Linq;
using UnityEngine;

public class FPSRayActive : MonoBehaviour
{
	public bool Sorting;

	public string[] IgnoreTag = new string[1]
	{
		"Player"
	};

	public string[] DestroyerTag = new string[1]
	{
		"Finish"
	};

	public void ShootRayOnce(Vector3 origin, Vector3 direction, int id, byte team)
	{
		if (Physics.Raycast(origin, direction, out RaycastHit hitInfo, 100f) && hitInfo.collider.gameObject != base.gameObject)
		{
			DamagePackage damagePackage = default(DamagePackage);
			damagePackage.Damage = 50;
			damagePackage.Normal = hitInfo.normal;
			damagePackage.Direction = direction;
			damagePackage.Position = hitInfo.point;
			damagePackage.ID = id;
			damagePackage.Team = team;
			damagePackage.DamageType = 0;
			hitInfo.collider.SendMessage("OnHit", damagePackage, SendMessageOptions.DontRequireReceiver);
		}
	}

	public void CheckingRay(Vector3 origin, Vector3 direction)
	{
		float maxDistance = 3f;
		RaycastHit[] array = Physics.RaycastAll(origin, direction, maxDistance);
		for (int i = 0; i < array.Length; i++)
		{
			if ((bool)array[i].collider)
			{
				RaycastHit raycastHit = array[i];
				raycastHit.collider.SendMessage("GetInfo", SendMessageOptions.DontRequireReceiver);
			}
		}
	}

	public void ActiveRay(Vector3 origin, Vector3 direction)
	{
		float maxDistance = 3f;
		RaycastHit[] array = Physics.RaycastAll(origin, direction, maxDistance);
		for (int i = 0; i < array.Length; i++)
		{
			if ((bool)array[i].collider)
			{
				array[i].collider.SendMessage("Pickup", base.gameObject, SendMessageOptions.DontRequireReceiver);
			}
		}
	}

	public void ActiveLocalRay(Vector3 origin, Vector3 direction)
	{
		float maxDistance = 3f;
		RaycastHit[] array = Physics.RaycastAll(origin, direction, maxDistance);
		for (int i = 0; i < array.Length; i++)
		{
			if ((bool)array[i].collider)
			{
				array[i].collider.SendMessage("PickupLocal", base.gameObject, SendMessageOptions.DontRequireReceiver);
			}
		}
	}

	public bool ShootSingleRay(Vector3 origin, Vector3 direction, byte num, byte spread, byte seed, byte damage, float size, int id, byte team)
	{
		bool result = false;
		System.Random random = new System.Random(seed);
		if (seed <= 0)
		{
			spread = 0;
		}
		DamagePackage damagePackage = default(DamagePackage);
		for (int i = 0; i < num; i++)
		{
			Vector3 direction2 = direction + new Vector3((float)random.Next(-spread, spread) * 0.001f, (float)random.Next(-spread, spread) * 0.001f, (float)random.Next(-spread, spread) * 0.001f);
			if (Physics.Raycast(origin, direction2, out RaycastHit hitInfo, size) && (bool)hitInfo.collider)
			{
				damagePackage.Damage = damage;
				damagePackage.Normal = hitInfo.normal;
				damagePackage.Direction = direction2;
				damagePackage.Position = hitInfo.point;
				damagePackage.ID = id;
				damagePackage.Team = team;
				damagePackage.DamageType = 0;
				hitInfo.collider.SendMessage("OnHit", damagePackage, SendMessageOptions.DontRequireReceiver);
				result = true;
			}
		}
		return result;
	}

	public bool ShootSingleRayTest(Vector3 origin, Vector3 direction, byte num, byte spread, byte seed, byte damage, float size, int id, byte team)
	{
		bool result = false;
		System.Random random = new System.Random(seed);
		if (seed <= 0)
		{
			spread = 0;
		}
		DamagePackage damagePackage = default(DamagePackage);
		for (int i = 0; i < num; i++)
		{
			Vector3 direction2 = direction + new Vector3((float)random.Next(-spread, spread) * 0.001f, (float)random.Next(-spread, spread) * 0.001f, (float)random.Next(-spread, spread) * 0.001f);
			if (Physics.Raycast(origin, direction2, out RaycastHit hitInfo, size) && (bool)hitInfo.collider)
			{
				damagePackage.Damage = damage;
				damagePackage.Normal = hitInfo.normal;
				damagePackage.Direction = direction2;
				damagePackage.Position = hitInfo.point;
				damagePackage.ID = id;
				damagePackage.Team = team;
				damagePackage.DamageType = 0;
				hitInfo.collider.SendMessage("OnHitTest", damagePackage, SendMessageOptions.DontRequireReceiver);
				result = true;
			}
		}
		return result;
	}

	public bool ShootRay(Vector3 origin, Vector3 direction, byte num, byte spread, byte seed, byte damage, float size, byte hitmax, int id, byte team)
	{
		bool result = false;
		System.Random random = new System.Random(seed);
		if (seed <= 0)
		{
			spread = 0;
		}
		DamagePackage damagePackage = default(DamagePackage);
		for (int i = 0; i < num; i++)
		{
			Vector3 direction2 = direction + new Vector3((float)random.Next(-spread, spread) * 0.001f, (float)random.Next(-spread, spread) * 0.001f, (float)random.Next(-spread, spread) * 0.001f);
			int num2 = 0;
			RaycastHit[] array = (from h in Physics.RaycastAll(origin, direction2, size)
				orderby h.distance
				select h).ToArray();
			for (int j = 0; j < array.Length; j++)
			{
				if ((bool)array[j].collider && tagCheck(array[j].collider.gameObject) && array[j].collider.gameObject != base.gameObject && (((bool)array[j].collider.transform.root && array[j].collider.transform.root != base.gameObject.transform.root && array[j].collider.transform.root.gameObject != base.gameObject) || array[j].collider.transform.root == null))
				{
					RaycastHit raycastHit = array[j];
					damagePackage.Damage = damage;
					damagePackage.Normal = raycastHit.normal;
					damagePackage.Direction = direction2;
					damagePackage.Position = raycastHit.point;
					damagePackage.ID = id;
					damagePackage.Team = team;
					damagePackage.DamageType = 0;
					raycastHit.collider.SendMessage("OnHit", damagePackage, SendMessageOptions.DontRequireReceiver);
					result = true;
					num2++;
					if (num2 >= hitmax || tagDestroyerCheck(raycastHit.collider.gameObject))
					{
						break;
					}
				}
			}
		}
		return result;
	}

	public bool ShootRayTest(Vector3 origin, Vector3 direction, byte num, byte spread, byte seed, byte damage, float size, byte hitmax, int id, byte team)
	{
		bool result = false;
		System.Random random = new System.Random(seed);
		if (seed <= 0)
		{
			spread = 0;
		}
		DamagePackage damagePackage = default(DamagePackage);
		for (int i = 0; i < num; i++)
		{
			Vector3 direction2 = direction + new Vector3((float)random.Next(-spread, spread) * 0.001f, (float)random.Next(-spread, spread) * 0.001f, (float)random.Next(-spread, spread) * 0.001f);
			int num2 = 0;
			RaycastHit[] array = (from h in Physics.RaycastAll(origin, direction2, size)
				orderby h.distance
				select h).ToArray();
			for (int j = 0; j < array.Length; j++)
			{
				if ((bool)array[j].collider && tagCheck(array[j].collider.gameObject) && array[j].collider.gameObject != base.gameObject && (((bool)array[j].collider.transform.root && array[j].collider.transform.root != base.gameObject.transform.root && array[j].collider.transform.root.gameObject != base.gameObject) || array[j].collider.transform.root == null))
				{
					RaycastHit raycastHit = array[j];
					damagePackage.Damage = damage;
					damagePackage.Normal = raycastHit.normal;
					damagePackage.Direction = direction2;
					damagePackage.Position = raycastHit.point;
					damagePackage.ID = id;
					damagePackage.Team = team;
					damagePackage.DamageType = 0;
					raycastHit.collider.SendMessage("OnHitTest", damagePackage, SendMessageOptions.DontRequireReceiver);
					result = true;
					num2++;
					if (num2 >= hitmax || tagDestroyerCheck(raycastHit.collider.gameObject))
					{
						break;
					}
				}
			}
		}
		return result;
	}

	public bool Overlap(Vector3 origin, Vector3 forward, byte damage, float size, float dot, int id, byte team)
	{
		bool result = false;
		Collider[] array = Physics.OverlapSphere(origin, size);
		Collider[] array2 = array;
		DamagePackage damagePackage = default(DamagePackage);
		foreach (Collider collider in array2)
		{
			if ((bool)collider && collider.gameObject != base.gameObject && collider.gameObject.transform.root != base.gameObject.transform)
			{
				UnityEngine.Debug.Log(collider.gameObject.transform.root.name);
				Vector3 normalized = (collider.transform.position - origin).normalized;
				float num = Vector3.Dot(normalized, forward);
				if (num >= dot)
				{
					damagePackage.Damage = damage;
					damagePackage.Normal = normalized;
					damagePackage.Direction = forward;
					damagePackage.Position = collider.gameObject.transform.position;
					damagePackage.ID = id;
					damagePackage.Team = team;
					damagePackage.DamageType = 0;
					collider.GetComponent<Collider>().SendMessage("OnHit", damagePackage, SendMessageOptions.DontRequireReceiver);
					result = true;
				}
			}
		}
		return result;
	}

	public bool OverlapTest(Vector3 origin, Vector3 forward, byte damage, float size, float dot, int id, byte team)
	{
		bool result = false;
		Collider[] array = Physics.OverlapSphere(origin, size);
		Collider[] array2 = array;
		foreach (Collider collider in array2)
		{
			if ((bool)collider && collider.gameObject != base.gameObject && collider.gameObject.transform.root != base.gameObject)
			{
				Vector3 normalized = (collider.transform.position - origin).normalized;
				float num = Vector3.Dot(normalized, forward);
				if (num >= dot)
				{
					result = true;
				}
			}
		}
		return result;
	}

	private bool tagDestroyerCheck(GameObject obj)
	{
		for (int i = 0; i < DestroyerTag.Length; i++)
		{
			if (obj.CompareTag(DestroyerTag[i]))
			{
				return true;
			}
		}
		return false;
	}

	private bool tagCheck(GameObject obj)
	{
		for (int i = 0; i < IgnoreTag.Length; i++)
		{
			if (obj.CompareTag(IgnoreTag[i]))
			{
				return false;
			}
		}
		return true;
	}
}
